﻿using Microsoft.Extensions.Configuration;
namespace CommandLib
{
    /// <summary>
    /// 配制文件读取类
    /// </summary>
    public static class AppSettings
    {
        private static IConfiguration configuration;
        private static string _webroot;
        /// <summary>
        /// 配制类初始化
        /// </summary>
        /// <param name="_configuration"></param>
        public static void Init(IConfiguration _configuration, string webroot = "") {
            configuration = _configuration;
            _webroot = webroot;
        }
        /// <summary>
        /// 获取WWWROOT路径
        /// </summary>
        public static string GetwwwRootPath { get { return _webroot; } }
        /// <summary>
        /// AppSettings节点读取配制
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetAppSetting(string key) {

          return configuration.GetSection($"AppSettings:{key}").Value;
          
        }
        /// <summary>
        /// 读取任意节点配制
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static IConfigurationSection GetSection(string key)
        {

            return configuration.GetSection(key);
        }
        /// <summary>
        /// 读取任意节点配制
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static T GetSection<T>(string key)
        {
            return configuration.GetSection(key).Get<T>();
        }

        /// <summary>
        /// 读取任意节点配制 泛型
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetSectionValue(string key)
        {
           
            return configuration.GetSection(key).Value;
        }
        /// <summary>
        /// 读取ConnectionStrings配制
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static string GetConnectionString(string name) {
            return configuration.GetConnectionString(name);
        }
    }

}
